#
# Makefile for e2scrub
#

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
top_builddir = ..
my_dir = scrub
INSTALL = @INSTALL@
MKDIR_P = @MKDIR_P@

@MCONFIG@

PROGS=		e2scrub e2scrub_all
MANPAGES=	e2scrub.8 e2scrub_all.8
CONFFILES=	e2scrub.conf

ifeq ($(HAVE_UDEV),yes)
UDEV_RULES	= e2scrub.rules
UDISKS_RULES	= ext4.rules
INSTALLDIRS_TGT	+= installdirs-udev
INSTALL_TGT	+= install-udev
UNINSTALL_TGT	+= uninstall-udev
endif

ifeq ($(HAVE_CROND),yes)
CRONTABS	= e2scrub_all.cron
LIBPROGS	+= e2scrub_all_cron
INSTALLDIRS_TGT	+= installdirs-crond installdirs-libprogs
INSTALL_TGT	+= install-crond install-libprogs
UNINSTALL_TGT	+= uninstall-crond uninstall-libprogs
endif

ifeq ($(HAVE_SYSTEMD),yes)
SERVICE_FILES	= e2scrub@.service e2scrub_all.service e2scrub_all.timer e2scrub_fail@.service e2scrub_reap.service
LIBPROGS	+= e2scrub_fail
INSTALLDIRS_TGT	+= installdirs-systemd installdirs-libprogs
INSTALL_TGT	+= install-systemd install-libprogs
UNINSTALL_TGT	+= uninstall-systemd uninstall-libprogs
endif

all:: $(PROGS) $(MANPAGES) $(CONFFILES) $(UDEV_RULES) $(UDISKS_RULES) $(SERVICE_FILES) $(CRONTABS) $(LIBPROGS)

e2scrub: $(DEP_SUBSTITUTE) e2scrub.in
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/e2scrub.in $@
	$(Q) chmod a+x $@

e2scrub_all: e2scrub_all.in
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/e2scrub_all.in $@
	$(Q) chmod a+x $@

e2scrub_fail: e2scrub_fail.in
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/e2scrub_fail.in $@
	$(Q) chmod a+x $@

e2scrub_all_cron: e2scrub_all_cron.in
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/e2scrub_all_cron.in $@
	$(Q) chmod a+x $@

%.8: %.8.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

%.conf: %.conf.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

%.rules: %.rules.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

%.service: %.service.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

%.cron: %.cron.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

%.timer: %.timer.in $(DEP_SUBSTITUTE)
	$(E) "	SUBST $@"
	$(Q) $(SUBSTITUTE_UPTIME) $< $@

installdirs-udev:
	$(E) "	MKDIR_P $(UDEV_RULES_DIR)"
	$(Q) $(MKDIR_P) $(DESTDIR)$(UDEV_RULES_DIR)

installdirs-crond:
	$(E) "	MKDIR_P $(CROND_DIR)"
	$(Q) $(MKDIR_P) $(DESTDIR)$(CROND_DIR)

installdirs-libprogs:
	$(E) "	MKDIR_P $(pkglibexecdir)"
	$(Q) $(MKDIR_P) $(DESTDIR)$(pkglibexecdir)

installdirs-systemd:
	$(E) "	MKDIR_P $(SYSTEMD_SYSTEM_UNIT_DIR)"
	$(Q) $(MKDIR_P) $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)

installdirs: $(INSTALLDIRS_TGT)
	$(E) "	MKDIR_P $(root_sbindir) $(man8dir) $(root_sysconfdir)"
	$(Q) $(MKDIR_P) $(DESTDIR)$(root_sbindir) \
		$(DESTDIR)$(man8dir) $(DESTDIR)$(root_sysconfdir)

install-udev: installdirs-udev
	$(Q) for i in $(UDEV_RULES); do \
		$(ES) "	INSTALL $(UDEV_RULES_DIR)/$$i"; \
		$(INSTALL_DATA) $$i $(DESTDIR)$(UDEV_RULES_DIR)/96-$$i; \
	done
	$(Q) for i in $(UDISKS_RULES); do \
		$(ES) "	INSTALL $(UDEV_RULES_DIR)/$$i"; \
		$(INSTALL_DATA) $$i $(DESTDIR)$(UDEV_RULES_DIR)/64-$$i; \
	done

install-crond: installdirs-crond
	$(Q) if test -n "$(CRONTABS)" ; then \
		$(ES) "	INSTALL $(CROND_DIR)/e2scrub_all" ; \
		$(INSTALL_DATA) e2scrub_all.cron $(DESTDIR)$(CROND_DIR)/e2scrub_all ; \
	fi

install-libprogs: $(LIBPROGS) installdirs-libprogs
	$(Q) for i in $(LIBPROGS); do \
		$(ES) "	INSTALL $(pkglibexecdir)/$$i"; \
		$(INSTALL_PROGRAM) $$i $(DESTDIR)$(pkglibexecdir)/$$i; \
	done

install-systemd: $(SERVICE_FILES) installdirs-systemd
	$(Q) for i in $(SERVICE_FILES); do \
		$(ES) "	INSTALL_DATA $(SYSTEMD_SYSTEM_UNIT_DIR)/$$i"; \
		$(INSTALL_DATA) $$i $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/$$i; \
	done

install-strip: install

install: $(PROGS) $(MANPAGES) $(FMANPAGES) installdirs $(INSTALL_TGT)
	$(Q) for i in $(PROGS); do \
		$(ES) "	INSTALL $(root_sbindir)/$$i"; \
		$(INSTALL_PROGRAM) $$i $(DESTDIR)$(root_sbindir)/$$i; \
	done
	$(Q) for i in $(MANPAGES); do \
		for j in $(COMPRESS_EXT); do \
			$(RM) -f $(DESTDIR)$(man8dir)/$$i.$$j; \
		done; \
		$(ES) "	INSTALL_DATA $(man8dir)/$$i"; \
		$(INSTALL_DATA) $$i $(DESTDIR)$(man8dir)/$$i; \
	done
	$(Q) for i in $(CONFFILES); do \
		$(ES) "	INSTALL_DATA $(root_sysconfdir)/$$i"; \
		$(INSTALL_DATA) $$i $(DESTDIR)$(root_sysconfdir)/$$i; \
	done

uninstall-udev:
	for i in $(UDEV_RULES); do \
		$(RM) -f $(DESTDIR)$(UDEV_RULES_DIR)/96-$$i; \
	done
	for i in $(UDISKS_RULES); do \
		$(RM) -f $(DESTDIR)$(UDEV_RULES_DIR)/64-$$i; \
	done

uninstall-crond:
	if test -n "$(CRONTABS)" ; then \
		$(RM) -f $(DESTDIR)$(CROND_DIR)/e2scrub_all ; \
	fi

uninstall-libprogs:
	for i in $(LIBPROGS); do \
		$(RM) -f $(DESTDIR)$(pkglibexecdir)/$$i; \
	done

uninstall-systemd:
	for i in $(SERVICE_FILES); do \
		$(RM) -f $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/$$i; \
	done

uninstall: $(UNINSTALL_TGT)
	for i in $(PROGS); do \
		$(RM) -f $(DESTDIR)$(root_sbindir)/$$i; \
	done
	for i in $(MANPAGES); do \
		$(RM) -f $(DESTDIR)$(man8dir)/$$i; \
	done
	for i in $(CONFFILES); do \
		$(RM) -f $(DESTDIR)$(root_sysconfdir)/$$i; \
	done

clean::
	$(RM) -f $(PROGS) $(MANPAGES) $(CONFFILES) $(UDEV_RULES) $(UDISKS_RULES) $(SERVICE_FILES) $(CRONTABS) $(LIBPROGS)

mostlyclean: clean
distclean: clean
	$(RM) -f .depend Makefile $(srcdir)/TAGS $(srcdir)/Makefile.in.old
